*** NOTE:  This do-file performs the specifications in Table 11c with APL = pp_nonpp ***



program mymlogit_uh_lf
	version 10.1
	args lnf bxj1 bxj2 loc_j1_l1 loc_j1_l2 mass_point loc_j2_l1 loc_j2_l2
	tempvar denom_l1 denom_l2 mass_prob_l1 mass_prob_l2
	quietly generate double `denom_l1' = 1 + exp(`bxj1' + `loc_j1_l1') + exp(`bxj2' + `loc_j2_l1')
	quietly generate double `denom_l2' = 1 + exp(`bxj1' + `loc_j1_l2') + exp(`bxj2' + `loc_j2_l2')

	quietly generate double `mass_prob_l1' = exp(`mass_point') / (1 + exp(`mass_point'))
	quietly generate double `mass_prob_l2' = 1 / (1 + exp(`mass_point'))


	quietly replace `lnf' = ln((`mass_prob_l1'*(1/`denom_l1')) + (`mass_prob_l2'*(1/`denom_l2'))) if $ML_y1 == 0
	quietly replace `lnf' = ln((`mass_prob_l1'*(exp(`bxj1' + `loc_j1_l1')/`denom_l1')) + (`mass_prob_l2'*(exp(`bxj1' + `loc_j1_l2')/`denom_l2'))) if $ML_y1 == 1
	quietly replace `lnf' = ln((`mass_prob_l1'*(exp(`bxj2' + `loc_j2_l1')/`denom_l1')) + (`mass_prob_l2'*(exp(`bxj2' + `loc_j2_l2')/`denom_l2'))) if $ML_y1 == 2
end



log using "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Logs\20110231 APL regs -- original size -- purch_arm -- pp_nonpp.log", replace



***********************
***********************
*** APL regressions ***
*** purch_arm       ***
*** pp_nonpp        ***
***********************
***********************



use "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Data\apl law variables for stata.dta", clear
sort origmonth state
save "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Data\apl law variables for stata.dta", replace

use "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Data\All 10 MSAs\all10 20 size both purch_arm generated.dta", clear
keep if outsample_a != 1 & fico > 0 & cltv != 0 & balance != 0
compress
sort origmonth state
save "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Data\All 10 MSAs\TEMP all10 20 size both purch_arm generated.dta", replace
merge origmonth state using "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Data\apl law variables for stata.dta"

egen big_loan_num = group(loan_id)

drop loan_id pp_pen pp_term state mba_stat currmonth origmonth mba_stat_lead1 outcome_b outsample_b outcome_c hpa init_rate orig_risk_prem subprime loan_id_num flip_loantype anti_steering

replace pp_nonpp = 0 if cover_purch == 0

generate apl_pre = pp_nonpp * prepay_pen
generate apl_pre_end = pp_nonpp * prepay_pen_end
*generate apl_doc = pp_nonpp * lownodoc
compress

generate msa_atl = 0
generate msa_bal = 0
generate msa_chi = 0
generate msa_los = 0
generate msa_mia = 0
generate msa_min = 0
generate msa_new = 0
generate msa_pho = 0
generate msa_pit = 0
generate msa_san = 0
replace msa_atl = 1 if msa_num == 1
replace msa_bal = 1 if msa_num == 2
replace msa_chi = 1 if msa_num == 3
replace msa_los = 1 if msa_num == 4
replace msa_mia = 1 if msa_num == 5
replace msa_min = 1 if msa_num == 6
replace msa_new = 1 if msa_num == 7
replace msa_pho = 1 if msa_num == 8
replace msa_pit = 1 if msa_num == 9
replace msa_san = 1 if msa_num == 10
compress

generate pre_atl = prepay_pen * msa_atl
generate pre_bal = prepay_pen * msa_bal
generate pre_chi = prepay_pen * msa_chi
generate pre_los = prepay_pen * msa_los
generate pre_mia = prepay_pen * msa_mia
generate pre_min = prepay_pen * msa_min
generate pre_new = prepay_pen * msa_new
generate pre_pho = prepay_pen * msa_pho
generate pre_pit = prepay_pen * msa_pit
generate pre_san = prepay_pen * msa_san
compress

generate pre_end_atl = prepay_pen_end * msa_atl
generate pre_end_bal = prepay_pen_end * msa_bal
generate pre_end_chi = prepay_pen_end * msa_chi
generate pre_end_los = prepay_pen_end * msa_los
generate pre_end_mia = prepay_pen_end * msa_mia
generate pre_end_min = prepay_pen_end * msa_min
generate pre_end_new = prepay_pen_end * msa_new
generate pre_end_pho = prepay_pen_end * msa_pho
generate pre_end_pit = prepay_pen_end * msa_pit
generate pre_end_san = prepay_pen_end * msa_san
compress

generate doc_atl = lownodoc * msa_atl
generate doc_bal = lownodoc * msa_bal
generate doc_chi = lownodoc * msa_chi
generate doc_los = lownodoc * msa_los
generate doc_mia = lownodoc * msa_mia
generate doc_min = lownodoc * msa_min
generate doc_new = lownodoc * msa_new
generate doc_pho = lownodoc * msa_pho
generate doc_pit = lownodoc * msa_pit
generate doc_san = lownodoc * msa_san
compress



*************************
*** APL variable only ***
*************************

*** Generating matrix of starting values using values from regression without APL variables ***
matrix starter = (0,-1.48636,0.205113,0.3279589,-0.0069439,0.0156477,1.535846,0.4642148,0.2426741,-0.7303535,0.1623441,-0.0025592,0.4522897,0.0098119,-0.0011071,-0.1280716,-0.2402593,-0.3137614,-0.4228447,0.0352198,-0.5025425,-1.621722,-1.160063,-0.8334536,0.1918242,-1.541667,-1.066676,-0.7288016,-1.980487,-2.5289,1.018326,1.018588,0.9815834,0.2196336,1.157866,1.29463,-0.0823337,0.9528839,0.8765657,0.7704222,0.7472611,0.9428633,-0.0872548,1.193859,0.9169813,0.5579621,1.046219,1.124506,0.4856131,-0.1823553,-0.0461718,-0.0768322,0.2446587,0.4066619,0.1143087,0.0752942,-0.0524499,0,-1.916839,0.3889349,0.0304438,-0.0011158,-0.019142,1.859802,1.109466,0.027954,-0.1327015,0.1759856,-0.0034088,0.2832379,-0.1819733,0.0420531,-0.2757797,-0.2707934,-0.5450968,-1.190925,-1.24613,-0.4148033,-1.112584,-0.4258063,-0.4522947,-0.424753,-0.5990048,-0.6497746,-0.1453023,-0.6971687,-1.615103,1.08756,0.9460802,0.4320091,-0.0589157,1.026905,0.4261436,-0.1046275,0.567992,0.4602392,1.08752,1.277987,1.277952,-0.1189084,1.444365,1.136395,0.434963,1.708507,1.465511,0.1982687,-0.0955243,0.1970835,0.0141698,0.0674922,0.2881813,-0.2152095,0.2173054,-0.0501243,-4.939647,2.298496,2.264492,-5.205797,1.390336)

*** Running mymlogit_uh_lf ***
ml model lf mymlogit_uh_lf (default: outcome_a = pp_nonpp prepay_pen prepay_pen_end lownodoc fico cltv payment_adj2 adj_1st post_adj_1st spread ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_6molibor vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (payoff: pp_nonpp prepay_pen prepay_pen_end lownodoc fico cltv payment_adj2 adj_1st post_adj_1st spread ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_6molibor vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (default_const1:) (default_const2:) (prob_coeff1:) (payoff_const1:) (payoff_const2:) if outsample_a != 1 & fico > 0 & cltv != 0 & balance != 0, cluster(big_loan_num)
*ml check
ml init starter, copy
ml maximize, iterate(25) difficult
outreg2 using "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic consistency\Stata\Stata Outregs\20110231 APL regs original size purch_arm pp_nonpp.txt", replace onecol text bracket(se) e(all)
*ml graph
do "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Do-files\mymlogit_uh_lf\Wald tests -- MSA-PLP interactions -- ARMs.do"

*** Saving the matrix of coefficient estimates ***
matrix a = e(b)

*** Creating starter matrices with 0 values for the APL*PLP interaction term coefficient estimates ***
*** -- This uses e(df_m), which is the number of explanatory variables in each equation, not       ***
***    including constant terms.                                                                   ***
*** -- For prepay_pen regressions, the APL variable is interacted with both prepay_pen and         ***
***    prepay_pen_end, requiring an extra pair of APL*PLP interaction term coefficient estimates.  ***
matrix afirst = a[1,1]
matrix asecond = a[1,2..(e(df_m)+1)]
matrix athird = a[1,(e(df_m)+2)..((e(df_m)*2)+5)]
matrix starter1 = (afirst,0,0,asecond,0,0,athird)
*matrix starter2 = (afirst,0,asecond,0,athird)



*********************************************
*** APL*prepay_pen and APL*prepay_pen_end ***
*********************************************

*** Running mymlogit_uh_lf ***
ml model lf mymlogit_uh_lf (default: outcome_a = pp_nonpp apl_pre apl_pre_end prepay_pen prepay_pen_end lownodoc fico cltv payment_adj2 adj_1st post_adj_1st spread ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_6molibor vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (payoff: pp_nonpp apl_pre apl_pre_end prepay_pen prepay_pen_end lownodoc fico cltv payment_adj2 adj_1st post_adj_1st spread ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_6molibor vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (default_const1:) (default_const2:) (prob_coeff1:) (payoff_const1:) (payoff_const2:) if outsample_a != 1 & fico > 0 & cltv != 0 & balance != 0, cluster(big_loan_num)
*ml check
ml init starter1, copy
ml maximize, iterate(25) difficult
outreg2 using "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic consistency\Stata\Stata Outregs\20110231 APL regs original size purch_arm pp_nonpp.txt", append onecol text bracket(se) e(all)
*ml graph
do "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Do-files\mymlogit_uh_lf\Wald tests -- MSA-PLP interactions -- ARMs.do"

/*

********************
*** APL*lownodoc ***
********************

*** Running mymlogit_uh_lf ***
ml model lf mymlogit_uh_lf (default: outcome_a = pp_nonpp apl_doc prepay_pen prepay_pen_end lownodoc fico cltv payment_adj2 adj_1st post_adj_1st spread ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_6molibor vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (payoff: pp_nonpp apl_doc prepay_pen prepay_pen_end lownodoc fico cltv payment_adj2 adj_1st post_adj_1st spread ageofloan ageofloan_2 rel_loan_size chg_unempl var_hpi var_6molibor vint2003 vint2004 vint2005 vint2006 judicial msa_atl msa_bal msa_chi msa_mia msa_min msa_new msa_pho msa_pit msa_san pre_atl pre_bal pre_chi pre_mia pre_min pre_new pre_pho pre_pit pre_san pre_end_atl pre_end_bal pre_end_chi pre_end_mia pre_end_min pre_end_new pre_end_pho pre_end_pit pre_end_san doc_atl doc_bal doc_chi doc_mia doc_min doc_new doc_pho doc_pit doc_san, noconstant) (default_const1:) (default_const2:) (prob_coeff1:) (payoff_const1:) (payoff_const2:) if outsample_a != 1 & fico > 0 & cltv != 0 & balance != 0, cluster(big_loan_num)
*ml check
ml init starter2, copy
ml maximize, iterate(25) difficult
outreg2 using "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic consistency\Stata\Stata Outregs\20110231 APL regs original size purch_arm pp_nonpp.txt", append onecol text bracket(se) e(all)
*ml graph
do "C:\Documents and Settings\morgan.rose\My Documents\Foreclosure\Geographic Consistency\Stata\Stata Do-files\mymlogit_uh_lf\Wald tests -- MSA-PLP interactions -- ARMs.do"

*/

log close



program drop mymlogit_uh_lf



clear



